//
//  ThirdViewController.m
//  UITestDemo
//
//  Created by Qianfeng on 15/8/14.
//  Copyright (c) 2015年 Qianfeng. All rights reserved.
//

#import "ThirdViewController.h"
#import "UserModel.h"
@interface ThirdViewController ()<UITextFieldDelegate>

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
-(void)initUI
{
    _nameField = [[UITextField alloc]initWithFrame:CGRectMake(20, 70, self.view.frame.size.width-40, 40)];
    _nameField.borderStyle = UITextBorderStyleRoundedRect;
    _nameField.placeholder = @"请输入用户名";
    
    [self.view addSubview:_nameField];
    
    _passWord = [[UITextField alloc]initWithFrame:CGRectMake(20, 120, self.view.frame.size.width-40, 40)];
    _passWord.borderStyle = UITextBorderStyleRoundedRect;
    _passWord.placeholder = @"请输入密码";
    [self.view addSubview:_passWord];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(20, 200, self.view.frame.size.width-40, 40);
    [button setTitle:@"登录" forState:UIControlStateNormal];
    button.titleLabel.textColor = [UIColor whiteColor];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.backgroundColor = [UIColor blueColor];
    [button addTarget:self action:@selector(logIn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
}
-(void)logIn
{
    if (![_nameField.text isEqual:@""]&&![_passWord.text  isEqual: @""]) {
        UserModel *model = [[UserModel alloc]init];
        model.name = _nameField.text;
        model.passWord = _passWord.text;
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [[user objectForKey:@"user"] addObject:model];
        [user synchronize];
    }
   
    _passWord.text = @"";
    _nameField.text = @"";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_passWord resignFirstResponder];
    [_nameField resignFirstResponder];
    [self.view resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
